/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.file;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.util.Log;
import android.view.View;
import android.widget.TextView;
import it.francescogabbrielli.android.solidalapp.BaseFragment;
import it.francescogabbrielli.android.solidalapp.EndpointsLoader;
import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.SolidalApp;
import it.francescogabbrielli.android.solidalapp.deviceendpoint.Deviceendpoint;
import it.francescogabbrielli.android.solidalapp.deviceendpoint.model.Device;
import it.francescogabbrielli.android.solidalapp.profileendpoint.model.Profile;

public abstract class FileFragment extends BaseFragment<FileFragmentListener> {
	
	protected void updateProfile(Profile profile, String msg, long time) {
		
		View view = getView();
		if (view==null)
			return;
		
		TextView msgText = (TextView) view.findViewById(R.id.file_message_text);
		TextView timeText = (TextView) view.findViewById(R.id.file_time_text);
		TextView nameText = (TextView) view.findViewById(R.id.profile_name_text);
		TextView phoneText = (TextView) view.findViewById(R.id.profile_phone_text);
		TextView creditsText = (TextView) view.findViewById(R.id.profile_credits_text);
		
		if (profile!=null) {
			
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					getString(R.string.simple_dateformat),
					Locale.getDefault());

			msgText.setText(msg);
			timeText.setText(dateFormat.format(new Date(time)));
			String name = profile.getUsername();
			if (name==null || "".equals(name))
				name = getString(R.string.anonymous_user);
			nameText.setText(name);
			phoneText.setText(profile.getDefaultPhone());
			creditsText.setText(profile.getCredits()!=null ? profile.getCredits().toString() : "");
			
		} else {
			
			msgText.setText("");
			timeText.setText("");
			nameText.setText("");
			phoneText.setText("");
			creditsText.setText("");
			
		}
	
	}
	
	protected EndpointsLoader<Profile> newLoader(final String regId) {
		return new EndpointsLoader<Profile>(getActivity(), Profile.class) {
			public Profile loadInBackground() {
				Device d = null;
				if (regId!=null)
					try {
						Deviceendpoint de = EndpointsUtils.buildEndpoint(Deviceendpoint.class);
						d = de.getDevice(regId).execute();
					} catch(Exception e) {
						Log.w(SolidalApp.TAG, "Cannot load response device", e);
					}
				Profile ret = super.loadInBackground();
				if (ret!=null && d!=null && d.getTelephoneNumber()!=null && !"".equals(d.getTelephoneNumber()))
					ret.setDefaultPhone(d.getTelephoneNumber());
				return ret;
			}
		};
	}
	
}
